<?php
	$tasks = mysql_connect('localhost','cs4350','!@'); 
	if (!$tasks) { 
		die('Could not connect to MySQL: ' . mysql_error()); 
	}
	if (!mysql_select_db('todo', $tasks)) {
		die('Aw snap, no database named todo...');
	}
	
	$sql = 'SELECT user FROM users WHERE user != "'.$_GET["user"].'"';
	$userResult = mysql_query($sql, $tasks);	
	
	if (!$userResult) {
		die($sql);
	}
?>

<html>
	<head>
		<?php
			echo '<script>';
			echo 'function verifyUser(){';
			echo 'var provided = "'. $_GET["user"] . '";';
			echo 'var login = sessionStorage.getItem("user");';
			echo 'if (login != provided){';
			echo 'window.location = "/index.html"';
			echo '}}';
			echo '</script>';
		?>
		<STYLE type = "text/css">
		<?php
			while ($userCSS = mysql_fetch_array($userResult)){
				echo '#user' . $userCSS['user'] . ' {position: absolute; z-index: 20; top: 90px; left: 150px; border-width: 3px; border-style: double; background: white; opacity: 0; padding:5px; width: 50%;}
				';
				echo '#show' . $userCSS['user'] . ':target ~ #user' . $userCSS['user'] . ' {z-index: 100; opacity: 1; transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -webkit-transition: opacity 1s ease-in-out;}
				';
				echo '#show' . $userCSS['user'] . ':target ~ #fade {z-index: 99; background-color: black; transition: background 1s ease-in-out; -moz-transition: background 1s ease-in-out; -webkit-transition: background 1s ease-in-out;}
				';
			}
		?>
		</style>
	
		<meta content "text/html; charset=utf-8" http-equiv="Content-Type">
		<title>Share My ToDo</title>
		<link rel="stylesheet" type="text/css" href="css/home.css" />
		<script src = "js/geo.js"></script>
	</head>
	<body>
	<section>
		<a class="hiddenanchor" id="showCreate"></a>
		<a class="hiddenanchor" id="hideCreate"></a>
		<a class="hiddenanchor" id="cancelEdit"></a>
		<?php
			$sql = 'SELECT user FROM users WHERE user != "'.$_GET["user"].'"';
			$taskHidden = mysql_query($sql, $tasks);	
			
			if (!$taskHidden) {
				die($sql);
			}
			while ($hidden = mysql_fetch_array($taskHidden)){
				echo '<a class="hiddenanchor" id="show' . $hidden['user'] . '"></a>';
			}
		?>
		
		<div id="fade"></div>
		<header>
			<?php 
			echo "<h1>" . $_GET["user"] . "'s ToDo</h1>";
			?>
			<a id = "logout" href="/index.html">Logout</a></br><br/>
		</header>
		<nav>
			<ul id="tabs">
				<?php
					echo '<li id="home"><a href="home.php?user='.$_GET["user"].'">Home</a></li>';
					echo '<li><a href = "#showCreate">Create</a></li>';
					echo '<li><a href="view.php?user='.$_GET["user"].'">View&nbsp;All</a></li>';
					echo '<li><a href="delete.php?user='.$_GET["user"].'">Delete</a></li>';
					echo '<li id="profile"><a href="profile.php?user='.$_GET["user"].'">Profile</a></li>';
					echo '<li class="active">Shared</li>';
				?>
			</ul>
		</nav>
		<article id = "main">
			<section id="title">
				<h2>Other's Lists</h2>
			</section>
			<section id="list">
			<?php
				$link = mysql_connect('localhost','cs4350','!@'); 
				if (!$link) { 
					die('Could not connect to MySQL: ' . mysql_error()); 
				}
				if (!mysql_select_db('todo', $link)) {
					die('Aw snap, no database named todo...');
				}
				
				$sql = 'SELECT user, pic FROM users WHERE user != "'.$_GET["user"].'"';
				$result = mysql_query($sql, $link);
				
				if (!$result) {
					die($sql);
				}
				while ($row = mysql_fetch_array($result)){
					if ($row["pic"] == NULL){
						echo '<img src="images/defualt.jpg" class="thumb"/>';
					} else {
						echo '<img src="profile/' . $row["pic"] . '" class="thumb"/>';
					}
					echo '<a href = "#show' . $row['user'] . '">' . $row['user'] . '</a>';
					echo '<br/><br/>';
				}
				mysql_close($link);
			?>
			</section>
		</article>
		<div id="create">
			<form action = "create.php" method = "post" class = "straight">
				<label for = "taskName">Task Name:</label>
					<input class="textbox" type="text" name="taskName" required="required"/><br/><br/>
				<label for = "taskDesc">Task Descrpition:</label><br/>
					<textarea class="textbox" name="taskDesc"; rows="5" cols="30" maxlength="250" /></textarea></br>
				<div id="local" class="small"></div>
				<input type="submit" value="Create" name="create" class="button" />
				<a href = "#hideCreate" class = "closeDialog"><input type="button" value="Cancel" name="cancel" class="button" /></a>
				<?php
					echo '<input type="hidden" value=' . $_GET["user"] . ' name="username" />';
				?>
			</form>
		</div>
		
		<?php
		
			$link = mysql_connect('localhost','cs4350','!@'); 
			if (!$link) { 
				die('Could not connect to MySQL: ' . mysql_error()); 
			}
			if (!mysql_select_db('todo', $link)) {
				die('Aw snap, no database named todo...');
			}
		
			$sql = 'SELECT user FROM users WHERE user != "'.$_GET["user"].'"';
			$userResults = mysql_query($sql, $link);	
			
			if (!$userResults) {
				die($sql);
			}
			while ($users = mysql_fetch_array($userResults)){
				echo '<div id="user' . $users['user'] . '">';
					
					$sqlTasks = 'SELECT * FROM list WHERE user="'. $users['user'] .'"';
					$userTasks = mysql_query($sqlTasks, $link);	
					if (!$userTasks) {
						die($sql);
					}
					while ($limitTask = mysql_fetch_array($userTasks)){
						echo $limitTask['taskName'] . '<br/>';
						echo '<div class="small">' . $limitTask['desc'] . '</div><br/>';
					}
					echo '<a href="#cancelEdit" class = "closeDialog"><input type="button" value="Close" name="cancel" class="button" /></a>';
				echo '</div>';
			}
			mysql_close($link);
		?>
		
		
	</section>
	</body>
</html>